<template>
  <div class="profile">
    <div class="page-header">
      <h2>个人资料</h2>
    </div>
    
    <el-row :gutter="20">
      <el-col :xs="24" :lg="8">
        <el-card>
          <div class="user-avatar">
            <el-avatar :size="120" :src="userInfo.avatar">
              <el-icon><User /></el-icon>
            </el-avatar>
            <h3>{{ userInfo.username }}</h3>
            <p>{{ userInfo.email }}</p>
          </div>
        </el-card>
      </el-col>
      
      <el-col :xs="24" :lg="16">
        <el-card>
          <template #header>
            <span>基本信息</span>
          </template>
          
          <el-form :model="userInfo" label-width="100px">
            <el-form-item label="用户名">
              <el-input v-model="userInfo.username" disabled />
            </el-form-item>
            
            <el-form-item label="邮箱">
              <el-input v-model="userInfo.email" />
            </el-form-item>
            
            <el-form-item label="昵称">
              <el-input v-model="userInfo.nickname" />
            </el-form-item>
            
            <el-form-item label="个人简介">
              <el-input
                v-model="userInfo.bio"
                type="textarea"
                :rows="3"
                placeholder="介绍一下您自己..."
              />
            </el-form-item>
            
            <el-form-item>
              <el-button type="primary" @click="updateProfile">
                保存修改
              </el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import { User } from '@element-plus/icons-vue'

const userStore = useUserStore()

const userInfo = reactive({
  username: userStore.user?.username || '',
  email: userStore.user?.email || '',
  nickname: userStore.user?.nickname || '',
  bio: userStore.user?.bio || '',
  avatar: userStore.user?.avatar || ''
})

const updateProfile = async () => {
  try {
    await userStore.updateProfile(userInfo)
    ElMessage.success('资料更新成功')
  } catch (error) {
    console.error('更新资料失败:', error)
    ElMessage.error('更新资料失败')
  }
}
</script>

<style scoped lang="scss">
.profile {
  padding: 20px;
}

.page-header {
  margin-bottom: 20px;
  
  h2 {
    margin: 0;
    color: var(--el-text-color-primary);
  }
}

.user-avatar {
  text-align: center;
  padding: 20px;
  
  h3 {
    margin: 20px 0 10px;
    color: var(--el-text-color-primary);
  }
  
  p {
    margin: 0;
    color: var(--el-text-color-regular);
  }
}
</style>